/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.view.family;

import java.util.List;

import uk.org.aspellclark.agenedb.R;
import uk.org.aspellclark.agenedb.gedcom.Gedcom;
import uk.org.aspellclark.agenedb.gedcom.model.Family;
import uk.org.aspellclark.agenedb.gedcom.model.Individual;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FamiliesListRowAdapter extends ArrayAdapter<Family> {

	static class FamiliesListRowAdapterViewHolder {
		public TextView tvActionStepId;
		public TextView tvActionStepName;
		public TextView tvActionStepStartTime;
		public CheckBox chkActionStepDone;
	}

	private List<Family> items;
	private LayoutInflater mInflater = (LayoutInflater) this.getContext().getSystemService(
			Context.LAYOUT_INFLATER_SERVICE);

	public FamiliesListRowAdapter(Context context, int textViewResourceId, List<Family> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			v = this.mInflater.inflate(R.layout.families_list_view_row, null);
		}
		Family o = this.items.get(position);
		if (o != null) {

			this.populateDates(v, o);

			this.populateHusband(v, Gedcom.getInstance().getIndividual(o.husband));
			this.populateWife(v, Gedcom.getInstance().getIndividual(o.wife));
			this.populateChildren(v, o.children);
		}
		return v;
	}

	private void populateChildren(View v, List<String> childrenIds) {
		List<Individual> children = Gedcom.getInstance().getIndividuals(childrenIds);
		try {
			ListView lstView = (ListView) v.findViewById(R.id.familiesListLayoutChildrenList);

			FamilyChildrenListRowAdapter indivAdapter = new FamilyChildrenListRowAdapter(this.getContext(),
					R.layout.family_children_list_view_row, children);

			//indivAdapter.sort(new IndividualSurnameNameComparator());
			lstView.setAdapter(indivAdapter);
			indivAdapter.notifyDataSetChanged();

			lstView.requestLayout();
		} catch (Exception e) {
			Toast.makeText(this.getContext(), "Exception Caught creating family children list", Toast.LENGTH_SHORT)
					.show();
		}
	}

	private void populateDates(View v, Family o) {
		TextView txtIndividualId = (TextView) v.findViewById(R.id.familiesListLayoutDateMarriage);
		if (txtIndividualId != null) {
			txtIndividualId.setText(o.getMarriageDate() + " ->" + o.getDivorceDate());
		}
	}

	private void populateHusband(View v, Individual husband) {
		if (husband != null) {
			TextView txtfirstname = (TextView) v.findViewById(R.id.familiesListLayoutHusbandTextNamesFirstname);
			if (txtfirstname != null) {
				txtfirstname.setText(husband.getNameFirst());
			}
			TextView txtlastname = (TextView) v.findViewById(R.id.familiesListLayoutHusbandTextNamesLastname);
			if (txtlastname != null) {
				txtlastname.setText("/" + husband.getNameLast() + "/");
			}
		}
	}

	private void populateWife(View v, Individual wife) {
		if (wife != null) {
			TextView txtfirstname = (TextView) v.findViewById(R.id.familiesListLayoutWifeTextNamesFirstname);
			if (txtfirstname != null) {
				txtfirstname.setText(wife.getNameFirst());
			}
			TextView txtlastname = (TextView) v.findViewById(R.id.familiesListLayoutWifeTextNamesLastname);
			if (txtlastname != null) {
				txtlastname.setText("/" + wife.getNameLast() + "/");
			}
		}
	}
}
